<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>selectionSort</title>
</head>

<body>
  <script>
    // 比较次数O(N^2)
    // 交换次数O(N)
    function swap(array, a, b) {
      [array[a], array[b]] = [array[b], array[a]]
    }

    function selectionSort(array) {
      let length = array.length
      for (let j = 0; j < length - 1; j++) {
        let min = j
        for (let i = min + 1; i < length; i++) {
          if (array[min] < array[i]) {
            min = i
          }
        }
        swap(array, min, j)
      }
      return array
    }
    let arr = [123, 14, 521, 8, 31, 943, 19, 56, 241]
    console.log(selectionSort(arr))
  </script>
</body>

</html>